// app/router.js
'use strict';

module.exports = app => {
  const { router, controller, middleware } = app;
  
  // 注册错误处理中间件
  const errorHandler = middleware.errorHandler(null, app);
  router.use(errorHandler);
  
  // 注册组织权限中间件
  const organizationPermission = middleware.organizationPermissionMiddleware(null, app);

  // 健进销项发票路由
  router.get('/api/v1/incoming-invoices', organizationPermission, controller.incomingInvoiceController.index);
  router.get('/api/v1/incoming-invoices/:id', organizationPermission, controller.incomingInvoiceController.show);
  router.post('/api/v1/incoming-invoices', organizationPermission, controller.incomingInvoiceController.create);
  router.put('/api/v1/incoming-invoices/:id', organizationPermission, controller.incomingInvoiceController.update);
  router.delete('/api/v1/incoming-invoices/:id', organizationPermission, controller.incomingInvoiceController.destroy);

  // 销项发票路由
  router.get('/api/v1/outgoing-invoices', organizationPermission, controller.outgoingInvoiceController.index);
  router.get('/api/v1/outgoing-invoices/:id', organizationPermission, controller.outgoingInvoiceController.show);
  router.post('/api/v1/outgoing-invoices', organizationPermission, controller.outgoingInvoiceController.create);
  router.put('/api/v1/outgoing-invoices/:id', organizationPermission, controller.outgoingInvoiceController.update);
  router.delete('/api/v1/outgoing-invoices/:id', organizationPermission, controller.outgoingInvoiceController.destroy);

  // 健康检查路由
  router.get('/health', controller.healthController.check);
  router.get('/performance', controller.healthController.performance);
  
  // 客户管理路由
  require('./router/customer_route')(app);
  
  // 销售订单相关路由
  router.get('/api/v1/sales-orders/stats', organizationPermission, controller.salesOrderController.getStats);
  router.get('/api/v1/sales-orders', organizationPermission, controller.salesOrderController.index);
  router.get('/api/v1/sales-orders/:id', organizationPermission, controller.salesOrderController.show);
  router.post('/api/v1/sales-orders', organizationPermission, controller.salesOrderController.create);
  router.put('/api/v1/sales-orders/:id', organizationPermission, controller.salesOrderController.update);
  router.delete('/api/v1/sales-orders/:id', organizationPermission, controller.salesOrderController.destroy);
  router.put('/api/v1/sales-orders/:id/collection-status', organizationPermission, controller.salesOrderController.updateCollectionStatus);
  router.post('/api/v1/sales-orders/import', organizationPermission, controller.salesOrderController.importSalesOrders);
  router.get('/api/v1/sales-orders/export-template', organizationPermission, controller.salesOrderController.exportTemplate);
  
  // 银行流水相关路由
  router.get('/api/v1/bank-transactions', organizationPermission, controller.bankTransactionController.index);
  router.get('/api/v1/bank-transactions/:id', organizationPermission, controller.bankTransactionController.show);
  router.post('/api/v1/bank-transactions', organizationPermission, controller.bankTransactionController.create);
  router.put('/api/v1/bank-transactions/:id', organizationPermission, controller.bankTransactionController.update);
  router.delete('/api/v1/bank-transactions/:id', organizationPermission, controller.bankTransactionController.destroy);
  
  // 对账相关路由
  router.post('/api/v1/reconciliation/smart-match', organizationPermission, controller.reconciliationController.generateAnalysisReport);
  router.post('/api/v1/reconciliation/manual-match', organizationPermission, controller.reconciliationController.generateAnalysisReport);
  router.put('/api/v1/reconciliation/confirm/:id', organizationPermission, controller.reconciliationResultController.confirm);
  router.put('/api/v1/reconciliation/reject/:id', organizationPermission, controller.reconciliationResultController.reject);
  router.get('/api/v1/reconciliation/results', organizationPermission, controller.reconciliationController.getReconciliationResults);
  router.get('/api/v1/reconciliation/results-new', organizationPermission, controller.reconciliationController.getReconciliationResults);
  router.post('/api/v1/reconciliation/rerun', organizationPermission, controller.reconciliationController.generateAnalysisReport);
  
  // 数据导入相关路由
  router.post('/api/v1/import/bank-transactions', organizationPermission, controller.importController.importBankTransactions);
  router.get('/api/v1/import/template/:type', organizationPermission, controller.importController.getTemplate);
  
  // 催款记录相关路由
  router.get('/api/v1/collection-records', organizationPermission, controller.collectionRecordController.index);
  router.get('/api/v1/collection-records/order', organizationPermission, controller.collectionRecordController.getRecordsByOrder);
  router.get('/api/v1/collection-records/:id', organizationPermission, controller.collectionRecordController.show);
  router.post('/api/v1/collection-records', organizationPermission, controller.collectionRecordController.create);
  router.put('/api/v1/collection-records/:id', organizationPermission, controller.collectionRecordController.update);
  router.delete('/api/v1/collection-records/:id', organizationPermission, controller.collectionRecordController.destroy);
  
  // 报告相关路由
  router.get('/api/v1/reports/reconciliation', organizationPermission, controller.reportController.generateReport);
  router.get('/api/v1/reports/statistics', organizationPermission, controller.reportController.getMatchStatistics);
  router.get('/api/v1/reports/customer-habits', organizationPermission, controller.reportController.getCustomerHabits);
  router.get('/api/v1/reports/suggestions', organizationPermission, controller.reportController.getSmartSuggestions);
  router.get('/api/v1/reports/collection-suggestions', organizationPermission, controller.reportController.getSmartCollectionSuggestions);
  // 添加建议执行跟踪路由
  router.get('/api/v1/reports/suggestions/tracking', organizationPermission, controller.reportController.getExecutionTracking);
  
  // 引入报告路由文件
  require('./router/report_route')(app);
  
  // 跨组织分析路由
  router.get('/api/v1/analysis/cross-organization/customers', organizationPermission, controller.crossOrganizationAnalysisController.getCustomerAnalysis);
  router.get('/api/v1/analysis/cross-organization/sales', organizationPermission, controller.crossOrganizationAnalysisController.getSalesAnalysis);
  router.get('/api/v1/analysis/cross-organization/financial', organizationPermission, controller.crossOrganizationAnalysisController.getFinancialAnalysis);
  router.get('/api/v1/analysis/cross-organization/suppliers', organizationPermission, controller.crossOrganizationAnalysisController.getSupplierAnalysis);
  
  // 库存管理相关路由
  // 产品类目路由
  router.get('/api/v1/inventory/categories/tree', organizationPermission, controller.productCategoryController.getCategoryTree);
  router.get('/api/v1/inventory/categories/list', organizationPermission, controller.productCategoryController.getCategoryList);
  
  // 品牌路由
  router.get('/api/v1/inventory/brands', organizationPermission, controller.brandController.getBrands);
  router.get('/api/v1/inventory/brands/:id', organizationPermission, controller.brandController.getBrand);
  router.post('/api/v1/inventory/brands', organizationPermission, controller.brandController.createBrand);
  router.put('/api/v1/inventory/brands/:id', organizationPermission, controller.brandController.updateBrand);
  router.delete('/api/v1/inventory/brands/:id', organizationPermission, controller.brandController.deleteBrand);
  
  // 引入库存路由文件
  require('./router/inventory_route')(app);
  
  // 采购管理相关路由
  require('./router/procurement_route')(app);
  
  // 对账管理相关路由
  require('./router/reconciliation_route')(app);
  
  // 组织管理相关路由
  router.get('/api/v1/organizations', organizationPermission, controller.organizationController.index);
  router.get('/api/v1/organizations/:id', organizationPermission, controller.organizationController.show);
  router.post('/api/v1/organizations', organizationPermission, controller.organizationController.create);
  router.put('/api/v1/organizations/:id', organizationPermission, controller.organizationController.update);
  router.delete('/api/v1/organizations/:id', organizationPermission, controller.organizationController.destroy);
  
  // 用户组织关联路由
  router.get('/api/v1/users/:userId/organizations', organizationPermission, controller.organizationController.getUserOrganizations);
  router.get('/api/v1/users/current/organizations', organizationPermission, controller.organizationController.getUserOrganizations);
  router.post('/api/v1/organizations/add-user', organizationPermission, controller.organizationController.addUserToOrganization);
  router.post('/api/v1/organizations/remove-user', organizationPermission, controller.organizationController.removeUserFromOrganization);
  router.get('/api/v1/users/:userId/organizations/:organizationId/role', organizationPermission, controller.organizationController.getUserRoleInOrganization);
  
  // 认证相关路由
  router.post('/api/v1/auth/login', controller.authController.login);
  router.post('/api/v1/auth/logout', controller.authController.logout);
  router.get('/api/v1/auth/current-user', controller.authController.getCurrentUser);
  
  // 系统管理相关路由
  router.get('/api/v1/system/users', organizationPermission, controller.systemController.getUsers);
  router.get('/api/v1/system/users/:id', organizationPermission, controller.systemController.getUser);
  router.post('/api/v1/system/users', organizationPermission, controller.systemController.createUser);
  router.put('/api/v1/system/users/:id', organizationPermission, controller.systemController.updateUser);
  router.delete('/api/v1/system/users/:id', organizationPermission, controller.systemController.deleteUser);
  router.post('/api/v1/system/users/batch-delete', organizationPermission, controller.systemController.batchDeleteUsers);
  router.post('/api/v1/system/users/batch-update-status', organizationPermission, controller.systemController.batchUpdateUserStatus);
  
  router.get('/api/v1/system/roles', organizationPermission, controller.systemController.getRoles);
  router.get('/api/v1/system/roles/:id', organizationPermission, controller.systemController.getRole);
  router.post('/api/v1/system/roles', organizationPermission, controller.systemController.createRole);
  router.put('/api/v1/system/roles/:id', organizationPermission, controller.systemController.updateRole);
  router.delete('/api/v1/system/roles/:id', organizationPermission, controller.systemController.deleteRole);
  
  router.get('/api/v1/system/logs', organizationPermission, controller.systemController.getOperationLogs);
  router.get('/api/v1/system/logs/:id', organizationPermission, controller.systemController.getOperationLog);
  router.delete('/api/v1/system/logs/:id', organizationPermission, controller.systemController.deleteOperationLog);
  
  router.get('/api/v1/system/settings', organizationPermission, controller.systemController.getSystemSettings);
  router.put('/api/v1/system/settings', organizationPermission, controller.systemController.updateSystemSettings);
  
  router.get('/api/v1/system/shortcuts', organizationPermission, controller.systemController.getUserShortcuts);
  router.put('/api/v1/system/shortcuts', organizationPermission, controller.systemController.updateUserShortcuts);
  
  // 用户分组相关路由
  router.get('/api/v1/system/user-groups', organizationPermission, controller.systemController.getUserGroups);
  router.post('/api/v1/system/user-groups', organizationPermission, controller.systemController.createUserGroup);
  router.put('/api/v1/system/user-groups/:id', organizationPermission, controller.systemController.updateUserGroup);
  router.delete('/api/v1/system/user-groups/:id', organizationPermission, controller.systemController.deleteUserGroup);
  router.get('/api/v1/system/user-groups/members', organizationPermission, controller.systemController.getGroupMembers);
  router.post('/api/v1/system/user-groups/add-user', organizationPermission, controller.systemController.addUserToGroup);
  router.post('/api/v1/system/user-groups/remove-user', organizationPermission, controller.systemController.removeUserFromGroup);
  
  // UI配置相关路由
  router.get('/api/v1/system/ui-config', organizationPermission, controller.systemController.getUIConfig);
  router.post('/api/v1/system/ui-config', organizationPermission, controller.systemController.saveUIConfig);
  router.delete('/api/v1/system/ui-config', organizationPermission, controller.systemController.deleteUIConfig);

  // 邀请相关路由
  router.post('/api/v1/invitations/generate', organizationPermission, controller.invitationController.generateInvitation);
  router.get('/api/v1/invitations/validate', controller.invitationController.validateInvitation);
  router.post('/api/v1/invitations/accept', organizationPermission, controller.invitationController.acceptInvitation);
  router.get('/api/v1/invitations/organization', organizationPermission, controller.invitationController.getOrganizationInvitations);
  router.delete('/api/v1/invitations/:id', organizationPermission, controller.invitationController.revokeInvitation);
  
  // 插件数据处理相关路由
  require('./router/plugin_data_route')(app);
};